/*
* @Author: majingyu
* @Date:   2017-05-24 20:41:31
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-07 20:16:10
*/
import Fetch from 'common/fetch';
import { getParams } from 'common/getParams';
//查找我的所有订单
const searchAllOrder = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'SEARCH_ALL_ORDER_SUCCESS'){
        action['data'] = data;
    }else if(type == 'SEARCH_ALL_ORDER_ERROR'){
        action['e'] = data;
    }
    return action;
}

const SEARCH_ALL_ORDER_BIGIN = 'SEARCH_ALL_ORDER_BIGIN';
const SEARCH_ALL_ORDER_SUCCESS = 'SEARCH_ALL_ORDER_SUCCESS';
const SEARCH_ALL_ORDER_ERROR = 'SEARCH_ALL_ORDER_ERROR';

export const searchMyOrderInfoAction = (params) => (dispatch, getState) => {
    dispatch(searchAllOrder(SEARCH_ALL_ORDER_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    let paramsList = getParams(params);
    return Fetch('/wx/order/orderPage?'+paramsList).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(searchAllOrder(SEARCH_ALL_ORDER_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(searchAllOrder(SEARCH_ALL_ORDER_ERROR,data));
        }
    }).catch(e=>{
        dispatch(searchAllOrder(SEARCH_ALL_ORDER_ERROR,e));
    })
}

//查看详情界面  START
//1、修改选中查看详情的ID
//修改信息ID
const CHANGE_ORDRT_DETAIL_ID = 'CHANGE_ORDRT_DETAIL_ID';
export const changeOrderDetailId = (id) => {
    return {
        type:CHANGE_ORDRT_DETAIL_ID,
        id
    }
}

//2、查看详情
//获取摸个某个的详情
const getOrderDetailtype = (type, data) => {
    let action = {
        type:type
    }
    if(type == GET_ORDER_DETAIL_SUCCESS){
        action['data'] = data;
    }else if(type == GET_ORDER_DETAIL_ERROR){
        action['e'] = data;
    }else if(type == GET_ORDER_DETAIL_BIGIN){
        action['data'] = data;
    }
    return action;
}
const GET_ORDER_DETAIL_BIGIN = 'GET_ORDER_DETAIL_BIGIN';
const GET_ORDER_DETAIL_SUCCESS = 'GET_ORDER_DETAIL_SUCCESS';
const GET_ORDER_DETAIL_ERROR = 'GET_ORDER_DETAIL_ERROR';

export const getOrderDetails = (id) => (dispatch, getState) => {
    let params = {
        orderId:id
    }
    dispatch(getOrderDetailtype(GET_ORDER_DETAIL_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/order/detail',{
        method:'POST',
         body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getOrderDetailtype(GET_ORDER_DETAIL_SUCCESS, data.data));
        }else{
            message.error(data.err)
            dispatch(getOrderDetailtype(GET_ORDER_DETAIL_ERROR,data.error));
        }
    }).catch(e=>{
        dispatch(getOrderDetailtype(GET_ORDER_DETAIL_ERROR,e));
    })
}

//删除订单的方法
const cacalOrders = (type, data) => {
    let action = {
            type:type
        }
    if(type == CACAL_ORDER_BYID_SUCCESS){
        action['data'] = data;
    }else if(type == CACAL_ORDER_BYID_ERROR){
        action['e'] = data;
    }
    return action;
}
const CACAL_ORDER_BYID_BEGIN = 'CACAL_ORDER_BYID_BEGIN';
const CACAL_ORDER_BYID_ERROR = 'CACAL_ORDER_BYID_ERROR';
const CACAL_ORDER_BYID_SUCCESS = 'CACAL_ORDER_BYID_SUCCESS';

export const cacalOrderById = (params) => (dispatch, getState) => {
    dispatch(cacalOrders(CACAL_ORDER_BYID_BEGIN));
    let myHeaders = new Headers();
    let url = params.status == 1?'wx/demand/delete':'wx/skill/delete';
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch(url+'?id='+params.id).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(cacalOrders(CACAL_ORDER_BYID_SUCCESS, data.data));

        }else{
            message.error(data.err)
            dispatch(cacalOrders(CACAL_ORDER_BYID_ERROR,data.error));
        }
    }).catch(e=>{
        dispatch(cacalOrders(CACAL_ORDER_BYID_ERROR,e));
    })
}